package swing.project.standard;

/**
 * @Title: StartSwing.java
 * @Description: TODO(StartSwing.java)
 * @author H819
 * @date 2010-10-6
 * @version V1.0
 */
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.commons.io.FileUtils;

import swings.mycomponent.SystemTrayFactory;
import swings.mycomponent.panel.JStatusBarPanel;

public class StartSwing {
	private static final ResourceBundle BUNDLE = ResourceBundle
			.getBundle("swing.project.standard.messages"); //$NON-NLS-1$

	// private Logger log = Logger.getLogger(StartSwing.class);

	private JFrame firstSwing;
	private JTabbedPane tabbedPane;
	private JPanel statuspanel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					StartSwing window = new StartSwing();
					// 增加 系统 systemtray
					SystemTrayFactory.createTray(window.firstSwing,
							"/swing/source/image/project/standard/user_go.png",
							BUNDLE.getString("app.title"));
					window.firstSwing.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public StartSwing() {
		initConfig();
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		firstSwing = new JFrame();

		firstSwing
				.setIconImage(Toolkit
						.getDefaultToolkit()
						.getImage(
								StartSwing.class
										.getResource("/swing/source/image/project/standard/user_go.png")));
		firstSwing.setResizable(false);
		firstSwing.setTitle(BUNDLE.getString("app.title"));
		firstSwing.setName("frame1");

		Point center = GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getCenterPoint();
		int windowWidth = 600;
		int windowHeight = 400;

		// 窗口居中
		firstSwing.setBounds(center.x - windowWidth / 2, center.y
				- windowHeight / 2, windowWidth, windowHeight);

		firstSwing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {

				// 提示选择的序号
				System.out.println("select tab "
						+ tabbedPane.getSelectedIndex() + " : "
						+ tabbedPane.getTitleAt(tabbedPane.getSelectedIndex()));

				// 逐个生成 tab 的内容组件，生成了一次，就不为 null 了，即使再次点击，也不再继续生成(加载 loadTab(n))
				if (tabbedPane.getSelectedComponent() == null) {
					// 获取所选Tab页
					System.out.println("new "
							+ tabbedPane.getTitleAt(tabbedPane
									.getSelectedIndex()) + " component ");
					int n = tabbedPane.getSelectedIndex();
					// 为指定标前页加载内容
					loadTab(n);

				}

			}
		});

		tabbedPane.setPreferredSize(new Dimension(500, 345));
		tabbedPane.setToolTipText(BUNDLE.getString("app.title"));
		firstSwing.getContentPane().add(tabbedPane, BorderLayout.NORTH);

		// 准备 tabbedPane

		tabbedPane
				.addTab(BUNDLE.getString("tab.name0"),
						createImageIcon("/swing/source/image/project/standard/book.gif"),
						null, BUNDLE.getString("tab.name0"));

//		tabbedPane
//				.addTab(BUNDLE.getString("tab.name2"),
//						createImageIcon("/swing/source/image/project/standard/book.gif"),
//						null, BUNDLE.getString("tab.name2"));
//
//		tabbedPane
//				.addTab(BUNDLE.getString("tab.name5"),
//						createImageIcon("/swing/source/image/project/standard/book.gif"),
//						null, BUNDLE.getString("tab.name5"));
//
//		tabbedPane
//				.addTab(BUNDLE.getString("tab.name6"),
//						createImageIcon("/swing/source/image/project/standard/book.gif"),
//						null, BUNDLE.getString("tab.name6"));
//
		tabbedPane
				.addTab(BUNDLE.getString("tab.name10"),
						createImageIcon("/swing/source/image/project/standard/book.gif"),
						null, BUNDLE.getString("tab.name10"));

		statuspanel = new JStatusBarPanel(
				BUNDLE.getString("app.statuspanel.leftmessage"),
				BUNDLE.getString("app.statuspanel.rightmessage"));
		firstSwing.getContentPane().add(statuspanel, BorderLayout.SOUTH);

	}

	/** Returns an ImageIcon, or null if the path was invalid. */
	protected static ImageIcon createImageIcon(String path) {
		URL imgURL = StartSwing.class.getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}

	// 为指定标签页加载内容
	private void loadTab(int n) {

		String title = tabbedPane.getTitleAt(n);

		if (title.equals(BUNDLE.getString("tab.name0"))) {

			tabbedPane.setComponentAt(n, new JPanelSwingWorkerCheckTilu());

//		} else if (title.equals(BUNDLE.getString("tab.name2"))) {
//
//			tabbedPane.setComponentAt(n, new Jpanel2());
//
//		} else if (title.equals(BUNDLE.getString("tab.name5"))) {
//			tabbedPane.setComponentAt(n, new Jpanel5());
//
//		} else if (title.equals(BUNDLE.getString("tab.name6"))) {
//			tabbedPane.setComponentAt(n, new Jpanel6());

		} else if (title.equals(BUNDLE.getString("tab.name10"))) {
			tabbedPane.setComponentAt(n, new Jpanel10());

		} else {

		}

		tabbedPane.setEnabledAt(n, true);
	}

	/**
	 * 初始化配置文件
	 */
	private void initConfig() {

		File config = new File(BUNDLE.getString("app.config.filepath"));

		if (!config.exists()) {
			List<String> ls = new ArrayList<String>();

			ls.add("config.excel.tilucheck.root=");
			ls.add("config.pdf.download.root=");

			try {
				FileUtils.writeLines(config, ls);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}
}
